<template>
    <div class="login-wrapper">
        <div v-if="userStore.userInfo.id">
            <h2 class="title">用户信息</h2>
            <div class="user-info">
                <div class="user-info-item">
                    <span class="user-info-label">登录用户名：</span>
                    <span class="user-info-value">{{ userStore.userInfo.mobile }}</span>
                </div>
                <div class="user-info-item">
                    <span class="user-info-label">昵称：</span>
                    <span class="user-info-value">{{ userStore.userInfo.nickname }}</span>
                </div>
                <div class="user-info-item">
                    <span class="user-info-label">上次登陆：</span>
                    <span class="user-info-value">{{ userStore.userInfo.login_time }}</span>
                </div>
                <div class="user-info-item">
                    <span class="user-info-label">建馆数：</span>
                    <span class="user-info-value">{{ userStore.userInfo.mu_total?userStore.userInfo.mu_total:0 }}</span>
                </div>
                <div class="user-info-item">
                    <span class="user-info-label">可用余额：</span>
                    <span class="user-info-value">{{ userStore.userInfo.user_money?userStore.userInfo.user_money:0 }}</span>
                </div>
                <div class="user-info-item">
                    <span class="user-info-label">可用永恒币：</span>
                    <span class="user-info-value">{{ userStore.userInfo.user_coin?userStore.userInfo.user_coin:0 }}</span>
                </div>
            </div>
            <div class="user-info-btn">
                <el-button type="primary" class="login-btn" size="mini" @click="goCenter"
                    >管理中心</el-button
                >
                <el-button type="primary" class="login-btn" size="mini" @click="logoutFun"
                    >退出登录</el-button
                >
            </div>
        </div>
        <div v-else>
            <h2 class="title">用户登录</h2>
            <el-form class="login-form" label-width="68px">
                <el-form-item label="用户名：">
                    <el-input v-model="form.account" placeholder="用户名、手机号" />
                </el-form-item>
                <el-form-item label="密&nbsp;&nbsp;码：">
                    <el-input v-model="form.password" placeholder="登录密码" type="password" />
                </el-form-item>
                <div class="options">
                    <el-checkbox v-model="form.remember">记住密码</el-checkbox>
                    <a href="#" class="forgot">忘记密码?</a>
                </div>
                <el-button type="primary" class="login-btn" size="large" @click="loginFun"
                    >登 录</el-button
                >
                <div class="register-tip" @click="goRegister">
                    注册用户，<span class="highlight">免费建立网上纪念馆</span>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script lang="ts" setup>
import { ElForm, ElFormItem, ElInput, ElCheckbox, ElButton } from 'element-plus'
import { ref } from 'vue'
import useUserStore from '@/stores/modules/user'
import { useRouter } from 'vue-router'
import { logout } from '@/api/user'
import { clearAuthInfo, getToken } from '@/utils/auth'
const userStore = useUserStore()
const router = useRouter()
const userInfo = ref()
userInfo.value = userStore.userInfo
const form = ref({
    account: '',
    password: '',
    remember: false
})
const loginFun = () => {
    console.log(form.value)
    userStore.login(form.value)
}
const goRegister = () => {
    router.push('/register')
}
const goCenter = () => {
    router.push({
        path: '/memorialManage'
    })
}
const logoutFun = () => {
    logout().then(() => {
        userStore.token = ''
        clearAuthInfo()
    }).catch(() => {
        
    })
    // userStore.logout()
}
</script>

<style scoped>
.login-wrapper {
    width: 100%;
    box-sizing: border-box;
    background-color: #f9f1d2;
    padding: 30px 20px;
    border-radius: 5px;
    font-family: 'Microsoft YaHei', sans-serif;
    color: #6b3d23;
    margin: 50px auto;
    box-shadow: 0 0 8px rgba(0, 0, 0, 0.1);
}

.title {
    font-size: 18px;
    text-align: center;
    margin-bottom: 20px;
    color: #8a3d19;
    font-weight: bold;
}

.login-form {
    display: flex;
    flex-direction: column;
}

.options {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
    font-size: 13px;
    color: #5e3b24;
}

.forgot {
    color: #8a3d19;
    text-decoration: none;
    font-size: 13px;
}

.login-btn {
    background-color: #a94f2f;
    border: none;
    width: 100%;
    font-size: 16px;
    letter-spacing: 2px;
    margin-bottom: 10px;
}

.register-tip {
    text-align: center;
    font-size: 13px;
    color: #8a3d19;
    cursor: pointer;
}

.highlight {
    color: #b33a16;
    font-weight: bold;
}
.user-info-btn{
	display: flex;
	justify-content: space-between;
	margin-top: 10px;
}
.user-info{
	display: flex;
	flex-direction: column;
	gap: 10px;
}
.user-info-item{
	display: flex;
}
</style>
